package com.four.project.service.impl;

import com.four.project.dao.TeacherMapper;
import com.four.project.dao.TeacherRepository;
import com.four.project.pojo.Teacher;
import com.four.project.pojo.TeacherResp;
import com.four.project.service.TeacherService;
import com.four.project.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *@author syk
 *@version 1.0
 *@since 2020-07-07 14:46
 */
@Service
public class TeacherServiceImpl implements TeacherService {
  @Autowired
    TeacherRepository teacherRepository;

  @Autowired
    TeacherMapper teacherMapper;
    @Override
    public String insert(Teacher teacher) {
        //加密
        String pw = MD5Util.toMD5(teacher.getPassword());
        teacher.setPassword(pw);
        Teacher save = teacherRepository.save(teacher);

        if (save!=null){
            return "success";
        }
        return "fail";
    }
    @Override
    public TeacherResp findAll(Integer page, Integer size) {
       //分页
        PageRequest pageRequest=new PageRequest(page-1,size);

        Page<Teacher> all=teacherRepository.findAll(pageRequest);

        TeacherResp tsp=new TeacherResp();

        //返回数据封装到对象中
        tsp.setTotal(all.getTotalElements());
        tsp.setList(all.getContent());
        return tsp;
    }

    @Override
    public Teacher updateTeacher(Teacher teacher) {
        //加密
        String pw = MD5Util.toMD5(teacher.getPassword());
        teacher.setPassword(pw);
        return teacherRepository.saveAndFlush(teacher);
    }

    @Override
    public void deleteById(Integer id) {
     teacherRepository.deleteById(id);
    }

    @Override
    public Integer updateMyMessage(Teacher teacher) {
        //加密
        String pw = MD5Util.toMD5(teacher.getPassword());
        teacher.setPassword(pw);
        return teacherMapper.updateMyMessage(teacher);
    }


}


